package com.cell.api.web.controllers;

import com.cell.api.application.dto.SendInput;
import com.cell.api.application.netty.utils.ChannelHashMap;
import com.cell.exception.ExceptionUtils;
import io.netty.channel.Channel;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author zdy
 * @date 2021/11/6
 */
@RestController
@RequestMapping("/netty/*")
public class NettyController {


    @PostMapping("send")
    public void login(@RequestBody @Validated SendInput send) {
        Channel channel = ChannelHashMap.getChannelByName(send.getClientId());
        if (channel == null || !channel.isActive()) {
            ExceptionUtils.throwUserFriendlyException("客户端失去连接，无法发送");
        }
        channel.writeAndFlush(send.getMsg());
    }
}
